package com.yl.chainStore.control.warehouse

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.warehouse.WarehouseCheckOrder
import com.yl.chainStore.pojo.vo.warehouse.WarehouseCheckOrderSelectVo
import com.yl.chainStore.services.warehouse.IWarehouseCheckOrderService
import com.yl.chainStore.services.warehouse.IWarehouseService
import com.yl.chainStore.services.IWebCompanyUserService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("warehouse/checkOrder")
open class WarehouseCheckOrderController(
	private val checkOrderService: IWarehouseCheckOrderService,
	private val iWarehouseService: IWarehouseService,
	private val iWebCompanyUserService: IWebCompanyUserService
) : BaseCtrl() {


	/**
	 * 盘点单列表
	 */
	@PostMapping("checkOrderPage")
	@PermsCode("warehouse:checkOrder:list")
	@Transactional
	open fun checkOrderPage(@RequestBody vo: WarehouseCheckOrderSelectVo): ResponseBody {
		val page = Page<WarehouseCheckOrder>(vo.current, vo.size)
		return returnSuccess(
			iWarehouseService.checkOrderPage(page, vo)
		)
	}


	/**
	 * 盘点单明细
	 */
	@GetMapping("checkOrderDetail/{id}")
	@PermsCode("warehouse:checkOrder:view")
	@Transactional
	open fun checkOrderDetail(@PathVariable id: Long): ResponseBody {
		val checkOrder = checkOrderService.getCheckOrder(id)
		if(checkOrder != null){
			checkOrder.details = checkOrderService.getOrderDetails(id)
		}
		return returnSuccess(
			checkOrder
		)
	}

	/**
	 * 暂存
	 */
	@PostMapping("staging")
	@Transactional(rollbackFor = [Exception::class])
	open fun staging(@RequestBody checkOrder: WarehouseCheckOrder): ResponseBody {
		if (checkOrder.id != 0L) {
			checkOrderService.deleteCheckOrder(checkOrder.id)
		}
		val res = iWarehouseService.addCheckOrder(checkOrder)

		if (res.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return res
		}
		var i = 1
		for (detail in checkOrder.details!!) {
			detail.checkOrderId = checkOrder.id
			detail.status = Status.STAGING
			val addDetail = checkOrderService.addDetail(detail)
			if (addDetail.code != ErrorCode.SUCCESS.code) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return returnError(ErrorCode.DATA_SAVE_FAILED, "第" + i + "行物品错误：" + addDetail.msg)
			}
			i++
		}
		return returnSuccess()
	}

	/**
	 * 盘点单添加
	 */
	@PostMapping("addCheckOrder")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("warehouse:checkOrder:add")
	open fun addCheckOrder(@RequestBody checkOrder: WarehouseCheckOrder): ResponseBody {
		if(checkOrder.id != 0L){
			checkOrderService.deleteCheckOrder(checkOrder.id)
		}
		val res = iWarehouseService.addCheckOrder(checkOrder)

		if (res.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return res
		}
		var i = 1
		for (detail in checkOrder.details!!) {
			detail.status = Status.WAIT_AUDIT
			detail.checkOrderId = checkOrder.id
			val addDetail = checkOrderService.addDetail(detail)
			if (addDetail.code != ErrorCode.SUCCESS.code) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return returnError(ErrorCode.DATA_SAVE_FAILED, "第" + i + "行物品错误：" + addDetail.msg)
			}
			i++
		}
		return returnSuccess()
	}

	@GetMapping("getStaging")
	@Transactional
	open fun getStaging() :ResponseBody{
		val order = iWebCompanyUserService.getStagingCheckOrder(getContext().userId, getContext().subCompanyId)
		if(order != null){
			order.details = checkOrderService.getOrderDetails(order.id)
		}
		return returnSuccess(order)
	}

	/**
	 * 调度单全部确认
	 */
	@PostMapping("completeCheckOrder/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("warehouse:checkOrder:audit")
	open fun completeCheckOrder(@PathVariable id: Long): ResponseBody {
		val checkOrder = checkOrderService.completeCheckOrder(id)
		if (checkOrder.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return checkOrder
	}

	/**
	 * 调度单 审核不通过
	 */
	@PostMapping("noPassCheckOrder/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("warehouse:checkOrder:audit")
	open fun noPassCheckOrder(@PathVariable id: Long): ResponseBody {
		val checkOrder = checkOrderService.noPassCheckOrder(id)
		if (checkOrder.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return checkOrder
	}

	/**
	 * 调度单全部取消
	 */
	@PostMapping("cancelCheckOrder/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun cancelCheckOrder(@PathVariable id: Long): ResponseBody {
		val checkOrder = checkOrderService.cancelCheckOrder(id)
		if (checkOrder.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return checkOrder
	}


}
